package com.blrs.service.activity.impl;

import java.sql.Timestamp;
import java.util.*;

import com.blrs.common.constant.Constants;
import com.blrs.common.constant.RouteIndex;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.core.domain.entity.SysUser;
import com.blrs.common.core.redis.RedisCache;
import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.MessageUtils;
import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.common.utils.uuid.UUID;
import com.blrs.domain.ActivityReception;
import com.blrs.domain.ActivityRegistration;
import com.blrs.domain.ActivityTraining;
import com.blrs.domain.vo.RegisterVO;
import com.blrs.mapper.*;
import com.blrs.service.activity.IActivityTrainingService;
import com.blrs.service.indexInfo.IInfoIndexService;
import com.blrs.system.mapper.SysDeptMapper;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.mapper.SysUserMapper;
import com.blrs.system.service.ISysDeptService;
import com.blrs.system.service.ISysUserService;
import com.blrs.utils.SmsHttpApi;
import com.blrs.utils.ThreadPoolTaskExecutorUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.blrs.utils.SmsHttpApi.sendMtMessage;

/**
 * 活动信息培训Service业务层处理
 * 
 * @author yang
 * @date 2023-11-22
 */
@Service
public class ActivityTrainingServiceImpl implements IActivityTrainingService
{
    @Autowired
    private ActivityTrainingMapper activityTrainingMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    @Autowired
    private ActivityReceptionMapper activityReceptionMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private ActivityRegistrationMapper activityRegistrationMapper;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IInfoIndexService infoIndexService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ThreadPoolTaskExecutorUtils threadPoolTaskExecutorUtils;




    /**
     * 查询活动信息培训
     * 
     * @param activityId 活动信息培训主键
     * @return 活动信息培训
     */
    @Override
    public ActivityTraining selectActivityTrainingByActivityId(String activityId)
    {
        return activityTrainingMapper.selectActivityTrainingByActivityId(activityId);
    }

    @Override
    public List<ActivityTraining> selectActivityTrainingByEnterpriseIdList(ActivityTraining activityTraining)
    {
        //接收方，需要通过接收id来查询当前用户能访问的数据列表
        activityTraining.setReceiverId(String.valueOf(SecurityUtils.getOrgId()));
        List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingByEnterpriseIdList(activityTraining);
        for (ActivityTraining training : activityTrainings) {
            //循环判断是否每条用户是否报了名,根据报名表中是否有报名记录
            if (activityReceptionMapper.selectIsPart(training.getActivityId(),SecurityUtils.getOrgId())>0){
                //说明报了名
                training.setPartRecive("是");
            }else {
                training.setPartRecive("否");
            }
        }
        return activityTrainings;
    }

    @Override
    public List<ActivityTraining> selectActivityTrainingByOrgId(ActivityTraining activityTraining) {
        activityTraining.setBelongTo(SecurityUtils.getOrgId());
        List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingByOrgId(activityTraining);
        for (ActivityTraining training : activityTrainings) {
            //统计该活动报名的企业数量
            training.setRegiEnterprise(activityReceptionMapper.selectActivityIsPartCount(training.getActivityId()));
            //training.setFileUrl( sysUploadFileMapper.selectUrlByCode("train",training.getFileUpload()));
        }
        return activityTrainings;
    }

    /**
     * 查询活动信息培训列表
     * 
     * @param activityTraining 活动信息培训
     * @return 活动信息培训
     */
    @Override
    public List<ActivityTraining> selectActivityTrainingList(ActivityTraining activityTraining)
    {
        activityTraining.setBelongTo(SecurityUtils.getOrgId());
        List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingList(activityTraining);
        for (ActivityTraining training : activityTrainings) {
            //统计该活动报名的企业数量
           training.setRegiEnterprise(activityReceptionMapper.selectActivityIsPartCount(training.getActivityId()));
           training.setFileUrl(String.valueOf(sysUploadFileMapper.selectUrlByCodeCount("train",training.getFileUpload())));
           training.setPartUrl(sysUploadFileMapper.selectUrlByCode("train",training.getPartUpload()));
           training.setPeoplesNum(activityRegistrationMapper.selectActivityRegiCount(training.getActivityId()));
        }
        return activityTrainings;
    }



    /**
     * 新增活动信息培训
     * 
     * @param activityTraining 活动信息培训
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertActivityTraining(ActivityTraining activityTraining) throws NoSuchFieldException, IllegalAccessException {
        //新增接收方数据
        activityTraining.setActivityId(UUID.randomUUID().toString().replaceAll("-", ""));
         if (activityTraining.getSendType().equals("groupSend")){
             for (String s : activityTraining.getEnterpriseId()) {
                 if (StringUtils.isNotEmpty(s)){
                     //先查询对应组织机构id
                     ActivityReception activityReception = new ActivityReception();
                     //根据企业id查询企业名称
                     String enterpriseName = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(s).getEnterpriseName();
//                     if (Objects.isNull(activityTraining.getUndertaker()) && Objects.isNull(activityTraining.getOrganizer())){
//                         //承办和协办都为空
//                         if (!activityTraining.getSponsor().contains(enterpriseName)){
//                             //根据企业名称查询组织机构id
//                             activityReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
//                             activityReception.setEnterpriseId(s);
//                             activityReception.setActivityId(activityTraining.getActivityId());
//                             activityReceptionMapper.insertActivityReception(activityReception);
//                         }
//                     }else if (Objects.isNull(activityTraining.getUndertaker()) && Objects.nonNull(activityTraining.getOrganizer())){
//                         //根据企业名称查询组织机构id
//                         if (!(activityTraining.getSponsor().contains(enterpriseName) || activityTraining.getOrganizer().contains(enterpriseName))){
//                             activityReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
//                             activityReception.setEnterpriseId(s);
//                             activityReception.setActivityId(activityTraining.getActivityId());
//                             activityReceptionMapper.insertActivityReception(activityReception);
//                         }
//                     }else if (Objects.nonNull(activityTraining.getUndertaker()) && Objects.isNull(activityTraining.getOrganizer())){
//                         if (!(activityTraining.getSponsor().contains(enterpriseName) || activityTraining.getUndertaker().contains(enterpriseName))){
//                             activityReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
//                             activityReception.setEnterpriseId(s);
//                             activityReception.setActivityId(activityTraining.getActivityId());
//                             activityReceptionMapper.insertActivityReception(activityReception);
//                         }
//                     }else {
//                         if (!(activityTraining.getSponsor().contains(enterpriseName) || activityTraining.getUndertaker().contains(enterpriseName) || activityTraining.getOrganizer().contains(enterpriseName))){
//                             //根据企业名称查询组织机构id
//                             activityReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
//                             activityReception.setEnterpriseId(s);
//                             activityReception.setActivityId(activityTraining.getActivityId());
//                             activityReceptionMapper.insertActivityReception(activityReception);
//                         }
//                     }
                             activityReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
                             activityReception.setEnterpriseId(s);
                             activityReception.setActivityId(activityTraining.getActivityId());
                             activityReceptionMapper.insertActivityReception(activityReception);

                 }
             }
             HashSet<String> strings = new HashSet<>();
             //获取协办单位
             String organizerId = activityTraining.getOrganizerId();
             //获取协办单位
             String undertakerId = activityTraining.getUndertakerId();
             //获取主办单位
             String sponsorId = activityTraining.getSponsorId();
             if (Objects.nonNull(undertakerId)){
                 String[] split2 = undertakerId.split(",");
                 for (String s1 : split2) {
                     strings.add(s1.trim());
                 }
             }
             if (Objects.nonNull(organizerId)){
                 String[] split = organizerId.split(",");
                 for (String s2 : split) {
                     strings.add(s2.trim());
                 }
             }
            if(Objects.nonNull(sponsorId)){
                String[] split1 = sponsorId.split(",");
                for (String sx : split1) {
                    strings.add(sx.trim());
                }
            }
             for (String string : strings) {
                 ActivityReception activityReception = new ActivityReception();
                 activityReception.setReceiverId(Long.valueOf(string));
                 activityReception.setActivityId(activityTraining.getActivityId());
                 activityReceptionMapper.insertActivityReception(activityReception);
             }
         }else {
             //说明是一键发送
             //获取所有的入驻企业id
             // 获取所有的入驻企业id
             List<SysDept> deptList = sysDeptMapper.selectOrgList(null);
             if (deptList != null && !deptList.isEmpty()) {
                 List<ActivityReception> activityReceptions = new ArrayList<>();
                 for (SysDept sysDept : deptList) {
                     ActivityReception activityReception = new ActivityReception();
                     activityReception.setActivityId(activityTraining.getActivityId());
                     activityReception.setReceiverId(sysDept.getDeptId());
                     activityReceptions.add(activityReception);
                 }
                 // 批量插入
                 activityReceptionMapper.batchInsertActivityReception(activityReceptions);
             }

         }
        ReflectionUtils.setCommonPrivateProperty(activityTraining);
         activityTraining.setBelongTo(SecurityUtils.getOrgId());
        activityTraining.setOrganizationName(SecurityUtils.getOrgName());
        //状态
        activityTraining.setStatus("审核");
        activityTraining.setReceiverId(String.join(",",activityTraining.getEnterpriseId()));
        return activityTrainingMapper.insertActivityTraining(activityTraining);
    }



    /**
     * 修改活动信息培训
     *
     * @param activityTraining 活动信息培训
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateActivityTraining(ActivityTraining activityTraining)
    {
        //可能在修改的时候，审核方已经审核了，所以需要查询数据库，在修改的同时，是否在审核
        if (!activityTrainingMapper.selectActivityTrainingByActivityId(activityTraining.getActivityId()).getStatus().equals("审核")){
            //不等于审核的时候，说明已经审核过了
           return AjaxResult.error("修改失败,活动已审核");
        }
      //判断传入的是一键发送还是分类群发
        if (activityTraining.getSendType().equals("groupSend")){
            //说明是分类群发,删除旧的接收表数据，添加新的
            activityReceptionMapper.deleteActivityReceptionByActivityId(activityTraining.getActivityId());
            for (String s : activityTraining.getEnterpriseId()) {
                if (StringUtils.isNotEmpty(s)){
                    //先查询对应组织机构id
                    ActivityReception activityReception = new ActivityReception();
                    //根据企业id查询企业名称
                    String enterpriseName = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(s).getEnterpriseName();
                    //根据企业名称查询组织机构id
                    activityReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
                    activityReception.setEnterpriseId(s);
                    activityReception.setActivityId(activityTraining.getActivityId());
                    activityReceptionMapper.insertActivityReception(activityReception);
                }
            }
            HashSet<String> strings = new HashSet<>();
            //获取协办单位
            String organizerId = activityTraining.getOrganizerId();
            //获取协办单位
            String undertakerId = activityTraining.getUndertakerId();
            //获取主办单位
            String sponsorId = activityTraining.getSponsorId();
            if (Objects.nonNull(undertakerId)){
                String[] split2 = undertakerId.split(",");
                for (String s1 : split2) {
                    strings.add(s1.trim());
                }
            }
            if (Objects.nonNull(organizerId)){
                String[] split = organizerId.split(",");
                for (String s2 : split) {
                    strings.add(s2.trim());
                }
            }
            String[] split1 = sponsorId.split(",");
            for (String sx : split1) {
                strings.add(sx.trim());
            }
            for (String string : strings) {
                ActivityReception activityReception = new ActivityReception();
                activityReception.setReceiverId(Long.valueOf(string));
                activityReception.setActivityId(activityTraining.getActivityId());
                activityReceptionMapper.insertActivityReception(activityReception);
            }
        }else {
            //说明是之前分类发送
            if (activityTrainingMapper.selectIsSend(activityTraining.getActivityId()).equals("groupSend")){
               //说明修改之前为分类群发
                activityReceptionMapper.deleteActivityReceptionByActivityId(activityTraining.getActivityId());
                //获取所有的入驻企业id
                List<SysDept> deptList = sysDeptMapper.selectOrgList(null);
                if (deptList != null && !deptList.isEmpty()) {
                    List<ActivityReception> activityReceptions = new ArrayList<>();
                    for (SysDept sysDept : deptList) {
                        ActivityReception activityReception = new ActivityReception();
                        activityReception.setActivityId(activityTraining.getActivityId());
                        activityReception.setReceiverId(sysDept.getDeptId());
                        activityReceptions.add(activityReception);
                    }
                    // 批量插入
                    activityReceptionMapper.batchInsertActivityReception(activityReceptions);
                }
            }
        }
        int i = activityTrainingMapper.updateActivityTraining(activityTraining);
        return AjaxResult.success();
    }

    /**
     * 批量删除活动信息培训
     * 
     * @param activityIds 需要删除的活动信息培训主键
     * @return 结果
     */
    @Override
    public int deleteActivityTrainingByActivityIds(String[] activityIds)
    {
        return activityTrainingMapper.deleteActivityTrainingByActivityIds(activityIds);
    }

    /**
     * 删除活动信息培训信息
     * 
     * @param activityId 活动信息培训主键
     * @return 结果
     */
    @Override
    public int deleteActivityTrainingByActivityId(String activityId)
    {
        return activityTrainingMapper.deleteActivityTrainingByActivityId(activityId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateActivityStatus(ActivityTraining activityTraining) {
        int i = activityTrainingMapper.updateActivityStatus(activityTraining);
        if (i>0){
            infoIndexService.createAndInsertInfoIndex(
                    activityTraining.getActivityId(),
                    activityTraining.getActivityName(),
                    activityTraining.getOrganizationName(),
                    activityTraining.getBelongTo(),
                    "活动培训",
                    RouteIndex.ACTIVITY_ROUTE,
                    2081L,
                    activityTraining.getSendType()
            );
           return AjaxResult.success("审核成功");
        }else {
            return AjaxResult.error("操作失败，请获取最新数据。");
        }
    }
    @Override
    public List<ActivityTraining> selectActivityTrainingListReciveList(ActivityTraining activityTraining) {
        //接收方，需要通过接收id来查询当前用户能访问的数据列表
        ArrayList<ActivityTraining> trainingArrayList = new ArrayList<>();
        activityTraining.setReceiverId(activityTraining.getSponsor());
        List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingListRecive(activityTraining);
        for (ActivityTraining training : activityTrainings) {
            //循环判断是否每条用户是否报了名,根据报名表中是否有报名记录
            if (activityReceptionMapper.selectIsPart(training.getActivityId(),Long.valueOf(activityTraining.getSponsor()))>0){
                //说明报了名
                training.setPartRecive("是");
                trainingArrayList.add(training);
            }
        }
        return trainingArrayList;
    }

    public boolean checkRedis(String phone, String code) {
        phone = "message:" + phone;
        String redisCode = (String) redisCache.getCacheObject(phone);
        if (redisCode == null) {
            return false; // 表示没有找到对应的缓存
        }
        return code.equals(redisCode); // 直接返回比较的结果
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult register(RegisterVO registerVO) {
        //检查验证码是否正确
        if (checkRedis(registerVO.getPhone(), registerVO.getCode())){
            //判断是否存在用户账号
            SysUser sysUser = sysUserService.selectUserByUserName(registerVO.getUserName());
            if (StringUtils.isNotNull(sysUser)){
                //存在用户账号
                return AjaxResult.error("该用户名称已存在，请重新输入！");
            }else {
                //获取新增后的默认机构id 根据机构企业名称查
            SysDept dept = new SysDept();
            dept.setParentId(Long.parseLong("0"));
            dept.setDeptName(registerVO.getDeptName());
            dept.setCompanyType("0");
            dept.setType("1");
            dept.setIsCertified("0");//是否认证
            dept.setDelFlag("0");
            dept.setStatus("0");
            dept.setCreateBy(registerVO.getUserName());
            dept.setCreateTime(new Timestamp(new Date().getTime()));
            SysDept deptList = sysDeptService.selectDeptByDeptName(dept);
                if (StringUtils.isNotNull(deptList)){
                    //存在该企业，新增用户
                    SysUser user = new SysUser();
                    user.setUserName(registerVO.getUserName());
                    user.setNickName(registerVO.getDeptName()+"_管理员");
                    user.setPassword(SecurityUtils.encryptPassword("123456"));
                    //默认为管理员
                    user.setIsAdmin("1");
                    user.setDelFlag("0");
                    user.setStatus("0");
                    user.setCreateBy(registerVO.getUserName());
                    user.setSex("0");
                    user.setCreateTime(new Timestamp(new Date().getTime()));
                    user.setDeptId(deptList.getDeptId());
                    user.setBelongTo(deptList.getDeptId());
                    boolean  regFlag= sysUserService.registerUser(user);
                    if (regFlag){
                        //新增报名表
                        ActivityRegistration activityRegistration = new ActivityRegistration();
                        activityRegistration.setApplicationId(UUID.randomUUID().toString().replaceAll("-", ""));
                        activityRegistration.setActivityId(registerVO.getActivityId());
                        activityRegistration.setPersonnelName(registerVO.getUserName());
                        activityRegistration.setPersonnelContact(registerVO.getPhone());
                        activityRegistration.setDeptId(deptList.getDeptId());
                        activityRegistration.setDeptName(registerVO.getDeptName());
                        activityRegistrationMapper.insertActivityRegistration(activityRegistration);
                        //异步发送短信
                        threadPoolTaskExecutorUtils.sendSms(registerVO.getPhone(),registerVO.getUserName(),registerVO.getActivityName());
                        return AjaxResult.success("报名成功！！！");
                    }else {
                        return AjaxResult.error("报名失败,请联系系统管理人员");
                    }
                }else {
                    //不存在该企业，新增企业
                    sysDeptService.insertDept(dept);
                    SysUser user = new SysUser();
                    //设置注册用户其他信息初始化
                    user.setUserName(registerVO.getUserName());
                    user.setNickName(registerVO.getDeptName()+"_管理员");
                    user.setPassword(SecurityUtils.encryptPassword("123456"));
                //默认为管理员
                    user.setIsAdmin("1");
                    user.setDelFlag("0");
                    user.setStatus("0");
                    user.setCreateBy(registerVO.getUserName());
                    user.setSex("0");
                    user.setCreateTime(new Timestamp(new Date().getTime()));
                //获取新增后的默认机构id 根据机构企业名称查
                SysDept deptNew = new SysDept();
                deptNew.setDeptName(registerVO.getDeptName());
                SysDept deptData = sysDeptService.selectDeptByDeptName(deptNew);
                    user.setDeptId(deptData.getDeptId());
                    user.setBelongTo(deptData.getDeptId());
                deptNew.setDeptId(deptData.getDeptId());
                deptNew.setBelongTo(deptData.getDeptId().toString());
                sysDeptService.updateDept(deptNew);
                boolean  regFlag= sysUserService.registerUser(user);
                    if (regFlag){
                    //新增接受表
                    ActivityReception activityReception = new ActivityReception();
                    activityReception.setActivityId(registerVO.getActivityId());
                    activityReception.setReceiverId(deptData.getDeptId());
                    activityReception.setIsPart(1L);
                    activityReceptionMapper.insertActivityReception(activityReception);
                    //新增报名表
                    ActivityRegistration activityRegistration = new ActivityRegistration();
                    activityRegistration.setApplicationId(UUID.randomUUID().toString().replaceAll("-", ""));
                    activityRegistration.setActivityId(registerVO.getActivityId());
                    activityRegistration.setPersonnelName(registerVO.getUserName());
                    activityRegistration.setPersonnelContact(registerVO.getPhone());
                    activityRegistration.setDeptId(deptData.getDeptId());
                    activityRegistration.setDeptName(registerVO.getDeptName());
                    activityRegistrationMapper.insertActivityRegistration(activityRegistration);
                    //异步发送短信
                    threadPoolTaskExecutorUtils.sendSms(registerVO.getPhone(),registerVO.getUserName(),registerVO.getActivityName());
                    return AjaxResult.success("报名成功！！！");
                }else {
                    return AjaxResult.error("报名失败,请联系系统管理人员");
                }
                }
            }
        }else {
            return AjaxResult.error("验证码错误，请重新输入！");
        }
    }



    @Override
    public List<ActivityTraining> selectActivityTrainingListRecive(ActivityTraining activityTraining) {
       //接收方，需要通过接收id来查询当前用户能访问的数据列表
        activityTraining.setReceiverId(String.valueOf(SecurityUtils.getOrgId()));
        activityTraining.setOrganizationName(SecurityUtils.getOrgName());
        List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingListRecive(activityTraining);
        for (ActivityTraining training : activityTrainings) {
            //循环判断是否每条用户是否报了名,根据报名表中是否有报名记录
            if (activityReceptionMapper.selectIsPart(training.getActivityId(),SecurityUtils.getOrgId())>0){
                //说明报了名
              training.setPartRecive("是");
            }else {
                training.setPartRecive("否");
            }
        }
        return activityTrainings;
    }

    @Override
    public List<ActivityTraining> selectActivityTrainingListExamnie(ActivityTraining activityTraining) {
//        if (SecurityUtils.getOrgId().equals(100L)){
//            activityTraining.setSendType("oneClick");
//            List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingList(activityTraining);
//            for (ActivityTraining training : activityTrainings) {
//                training.setFileUrl(sysUploadFileMapper.selectUrlByCode("policy",training.getFileUpload()));
//            }
//            return activityTrainings;
//        }else {
//            activityTraining.setSendType("groupSend");
//            activityTraining.setBelongTo(SecurityUtils.getOrgId());
//            activityTraining.setExamineId(SecurityUtils.getUserId());
//            List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingList(activityTraining);
//            for (ActivityTraining activity : activityTrainings) {
//                activity.setFileUrl(sysUploadFileMapper.selectUrlByCode("policy",activity.getFileUpload()));
//            }
//            return activityTrainings;
//        }

        if (SecurityUtils.getOrgId().equals(100L)){
            //判断在审核之前是否修改过数据
            List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingListExamine(activityTraining);
            for (ActivityTraining training : activityTrainings) {
                int i = sysUploadFileMapper.selectUrlByCodeCount("train", training.getFileUpload());
                training.setFileUrl(String.valueOf(i));
            }
            return activityTrainings;
        }else {
            return Collections.emptyList();
        }
    }

    @Override
    public int updateActivityTrainingScore(ActivityTraining activityTraining) {
        return activityTrainingMapper.updateScore(activityTraining);
    }

    @Override
    public List<ActivityTraining> selectActivityTrainingPublicList(ActivityTraining activityTraining) {
        List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingPublicList();
        for (ActivityTraining training : activityTrainings) {
          training.setActivityPicture(  sysUploadFileMapper.selectUrlByCode("train",training.getActivityPicture()));
        }
        return activityTrainings;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult terminationActivityTraining(ActivityTraining activityTraining) {
        int i = activityTrainingMapper.updateActivityTrainingByStatus(activityTraining);
        if (i>0){
            activityReceptionMapper.deleteActivityReceptionByActivityId(activityTraining.getActivityId());
           return AjaxResult.success("终止成功");
        }else {
            return AjaxResult.error("操作失败，请获取最新数据。");
        }
    }

    @Override
    public List<ActivityTraining> selectActivityTrainingListTopThree(Long orgId) {
      return   activityTrainingMapper.selectActivityTrainingListTopThree(orgId);
    }

    @Override
    public List<String> selectActivityTypeList() {
     List<String> list=activityTrainingMapper.selectActivityTypeList();
     list.add("不限");
     return list;
    }

    @Override
    public List<ActivityTraining> selectActivityTypeOneList(ActivityTraining activityTraining) {
        List<ActivityTraining> list=activityTrainingMapper.selectActivityTrainingOneList(activityTraining);
        for (ActivityTraining training : list) {
            training.setLogoUrl(sysUploadFileMapper.selectUrlByCode("enterprise_logo",String.valueOf(training.getBelongTo())));
            training.setActivityPicture(sysUploadFileMapper.selectUrlByCode("train",training.getActivityPicture()));
        }
        return list;
    }

    @Override
    public List<ActivityTraining> selectActivityTrainingByActivityIdFive(String activityId) {
        List<ActivityTraining> activityTrainings = activityTrainingMapper.selectActivityTrainingListById(activityId);
        for (ActivityTraining activityTraining : activityTrainings) {
            activityTraining.setActivityPicture(sysUploadFileMapper.selectUrlByCode("train",activityTraining.getActivityPicture()));
        }
        return activityTrainings;
    }

}
